package com.shd.edanpei.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.base.TranBarActivity;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.UserBean;
import com.shd.edanpei.customer.activity.CMainActivity;
import com.shd.edanpei.db.DaoManager;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;


/**
 * Created by mc on 2018/9/6.
 * 启动页
 */
@SuppressWarnings("ALL")
public class SplashActivity extends Activity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        toActivity();
    }


    private void toActivity() {
        UserBean userBean = DaoManager.getInstance().getUserBean();
        if (userBean != null && !TextUtils.isEmpty(userBean.getAccessToken())) {
            loginVerify(userBean);
        } else {
            startActivity(LoginActivity.class);
            finish();
        }
    }


    private void loginVerify(UserBean bean) {
        if (bean.getState() == RegisterResultActivity.freeze) {
            //冻结
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.TYPE, bean.getState());
            startActivity(RegisterResultActivity.class, bundle);
        } else {
            //未冻结
            switch (bean.getRoleType()) {
                case 0:
                    //未知角色
                    startActivity(RoleChoiceActivity.class);
                    break;
                case 1:
                    //C端用户
                    startActivity(CMainActivity.class);
                    break;
                case 4:
                case 5:
                    startActivity(MainActivity.class);
                    break;
                default:
                    //其他角色
                    if (bean.isReal()) {
                        //已实名认证
                        if (bean.isPerfectInfo()) {
                            //已完善资料
                            if (bean.getState() == RegisterResultActivity.normal) {
                                startActivity(MainActivity.class);
                            } else if (bean.getState() == RegisterResultActivity.apply
                                    || bean.getState() == RegisterResultActivity.refuse
                                    || bean.getState() == RegisterResultActivity.freeze) {
                                Bundle bundle = new Bundle();
                                bundle.putInt(Constant.TYPE, bean.getState());
                                startActivity(RegisterResultActivity.class, bundle);
                            }
                        } else {
                            //未完善资料
                            startActivity(PerfectDataActivity.class);
                        }
                    } else {
                        //未实名认证
                        Bundle bundle = new Bundle();
                        bundle.putInt(Constant.FROM, CertificationActivity.FROM_LOGIN);
                        startActivity(CertificationActivity.class, bundle);
                    }
                    break;
            }
        }
        finish();
    }

    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

}
